package org.pcorp.battlespace.persistance.dao.impl;

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.hibernate.SessionFactory;
import org.pcorp.battlespace.persistance.dao.EquipementDao;
import org.pcorp.battlespace.persistance.modele.Equipement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * Home object for domain model class Equipements.
 * @see org.pcorp.battlespace.persistance.modele.construction.modele.dao.Equipements
 * @author Hibernate Tools
 */
@Repository
public class EquipementJpaDao extends HibernateDaoSupport implements EquipementDao{

	private static final Logger log = LoggerFactory.getLogger(EquipementJpaDao.class);

	@Override
	@Autowired
	public void initDao(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}


	@Override
	public void persist(Equipement equipement) {
		log.debug("persisting Equipements instance");
		try {
			getHibernateTemplate().saveOrUpdate(equipement);
			log.debug("persist successful");
		}
		catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Equipement equipement) {
		log.debug("deleting Equipements instance");
		try {
			getHibernateTemplate().delete(equipement);
			log.debug("delete successful");
		}
		catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public Equipement findById(long id) {
		log.debug("getting Equipements instance with id: " + id);
		try {
			Equipement instance = (Equipement) getHibernateTemplate().get("org.pcorp.battlespace.construction.modele.dao.Equipements", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			}
			else {
				log.debug("get successful, instance found");
			}
			return instance;
		}
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List<Equipement> findByExample(Equipement instance) {
		log.debug("finding Equipements instance by example");
		try {
			@SuppressWarnings("unchecked")
			List<Equipement> results = (List<Equipement>) getSession().createCriteria("org.pcorp.battlespace.construction.modele.dao.Equipements").add(create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		}
		catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
